const vm = new Vue({
    el: '#main',
    data: {
        accountList: [],
    },
    methods: {
        deleteAccount,
        setDefaultAccount,
    },
});

function apiready() {
    fxj.fixLayout();
    reqAccountList();
}
async function reqAccountList() {
    const res = await ajax({url: 'home/userCenter/getAccountList'});
    if (res.data instanceof Array) {
        res.data.map(account => {
            account.account_type = getBankType(account.account_type);
        });
        vm.accountList = res.data;
    }
}

function getBankType(type) {
    const bank = {
        '0': '支付宝',
        '1': '银行卡',
        '3': '工商银行',
        '4': '建设银行',
        '5': '中国银行',
        '6': '农业银行',
        '7': '交通银行',
        '8': '招商银行',
        '9': '中信银行',
        '10': '民生银行',
        '11': '兴业银行',
        '12': '浦发银行',
        '13': '邮政储蓄',
        '14': '光大银行',
        '15': '平安银行',
        '16': '华夏银行',
        '17': '北京银行',
        '18': '广发银行',
    };
    return bank[type];
}

async function deleteAccount(id) {
    const close = fxj.dialog.loading();
    const res = await ajax({
        url: 'home/userCenter/accountManagement',
        data: {
            type: '2',
            upid: id,
        },
    });
    close();
    mui.toast(res.msg);
    if (res.err === 0) {
        reqAccountList();
    }
}

async function setDefaultAccount(id) {
    const close = fxj.dialog.loading();
    const res = await ajax({
        url: 'home/userCenter/accountManagement',
        data: {
            type: '3',
            upid: id,
        },
    });
    close();
    mui.toast(res.msg);
    if (res.err === 0) {
        reqAccountList();
    }
}

function closePage() {
    fxj.closeWin('upload_idcard');
    fxj.closeWin('upload_idcard_front');
}